#!/usr/bin/env bash

# Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

if [ -n "${feature_musl}" ]; then
    if [ -n "${feature_ohos}" ]; then
        triple=${arch}-linux-ohos
    else
        triple=${arch}-unknown-linux-musl
    fi
else
    triple=${arch}-unknown-linux-gnu
fi

make DESTDIR=${install_root} -C build install || exit 1

if [ ! -z "${feature_standalone_clangrt}" ]; then
    mkdir -p ${install_root}/usr/lib/clang/19/lib/${triple} || exit 1
    mv ${install_root}/usr/lib/linux/clang_rt.crtbegin-${arch}.o ${install_root}/usr/lib/clang/19/lib/${triple}/clang_rt.crtbegin.o || exit 1
    mv ${install_root}/usr/lib/linux/clang_rt.crtend-${arch}.o  ${install_root}/usr/lib/clang/19/lib/${triple}/clang_rt.crtend.o || exit 1
    mv ${install_root}/usr/lib/linux/libclang_rt.builtins-${arch}.a ${install_root}/usr/lib/clang/19/lib/${triple}/libclang_rt.builtins.a || exit 1
    mv ${install_root}/usr/lib/linux/libclang_rt.profile-${arch}.a ${install_root}/usr/lib/clang/19/lib/${triple}/libclang_rt.profile.a || exit 1
    rm -r ${install_root}/usr/include || exit 1
    rm -r ${install_root}/usr/lib/linux || exit 1
    exit 0
fi

if [ ! -z "${feature_standalone_llvmrt}" ]; then
    # fix clang_rt paths
    mkdir -p ${install_root}/usr/lib/clang/19/lib/${triple} || exit 1
    mv ${install_root}/usr/lib/linux/clang_rt.crtbegin-${arch}.o ${install_root}/usr/lib/clang/19/lib/${triple}/clang_rt.crtbegin.o || exit 1
    mv ${install_root}/usr/lib/linux/clang_rt.crtend-${arch}.o  ${install_root}/usr/lib/clang/19/lib/${triple}/clang_rt.crtend.o || exit 1
    mv ${install_root}/usr/lib/linux/libclang_rt.builtins-${arch}.a ${install_root}/usr/lib/clang/19/lib/${triple}/libclang_rt.builtins.a || exit 1
    mv ${install_root}/usr/lib/linux/libclang_rt.profile-${arch}.a ${install_root}/usr/lib/clang/19/lib/${triple}/libclang_rt.profile.a || exit 1

    # fix lib paths
    mkdir -p ${install_root}/usr/lib/${triple} || exit 1
    mv ${install_root}/usr/lib/libarcher* ${install_root}/usr/lib/${triple}/ || exit 1
    mv ${install_root}/usr/lib/libc++* ${install_root}/usr/lib/${triple}/ || exit 1
    mv ${install_root}/usr/lib/lib*omp* ${install_root}/usr/lib/${triple}/ || exit 1
    mv ${install_root}/usr/lib/libunwind* ${install_root}/usr/lib/${triple}/ || exit 1
    rm -r ${install_root}/usr/lib/linux || exit 1
    
    # fix cmake paths
    mkdir -p ${install_root}/usr/lib/${triple}/cmake || exit 1
    mv ${install_root}/usr/lib/cmake/openmp ${install_root}/usr/lib/${triple}/cmake/ || exit 1

    # fix clang include paths
    mkdir -p ${install_root}/usr/lib/clang/19/include || exit 1
    mv ${install_root}/usr/include/profile  ${install_root}/usr/lib/clang/19/include/ || exit 1

    # fix libcxx include paths
    mkdir -p ${install_root}/usr/include/${triple}/c++/v1 || exit 1
    mv ${install_root}/usr/include/c++/v1/__config_site ${install_root}/usr/include/${triple}/c++/v1/ || exit 1

    exit 0
fi

if [ ! -z "${feature_standalone_flangrt}" ]; then
    mv /usr/lib/libFortranRuntime.a ${install_root}/usr/lib/ || exit 1
    exit 0
fi

ln -sfv lld ${install_root}/usr/bin/ld || exit 1

# install missing native tools for pure build
if [ ${impure} -eq 1 ]; then
    install -m 755 build/bin/clang-tidy-confusable-chars-gen ${install_root}/usr/bin
    install -m 755 build/bin/lldb-tblgen ${install_root}/usr/bin
    install -m 755 build/bin/llvm-min-tblgen ${install_root}/usr/bin
    install -m 755 build/bin/clang-pseudo-gen ${install_root}/usr/bin
    install -m 755 build/bin/mlir-src-sharder ${install_root}/usr/bin
fi

# install missing flang mods for host toolchain
# flang mods are not installed when cross-compiling
if [ ${host_toolchain} -eq 1 ]; then
    cp -P ${install_root}-impure/usr/include/flang/*.mod ${install_root}/usr/include/flang/ || exit 1

    # FIXME: how to install flang mods for different architectures?
fi

# create ld config
mkdir -p ${install_root}/etc/ld.so.conf.d || exit 1
echo "include /etc/ld.so.conf.d/*.conf" > ${install_root}/etc/ld.so.conf || exit 1
cat > ${install_root}/etc/ld.so.conf.d/${triple}.conf <<EOF
/usr/lib/${triple}
/host/usr/lib/${triple}
EOF
